package com.qike.baoxiu.service;

import com.qike.baoxiu.enums.StatusEnums;
import com.qike.baoxiu.mapper.MessageMapper;
import com.qike.baoxiu.mapper.UserMapper;
import com.qike.baoxiu.pojo.Message;
import com.qike.baoxiu.pojo.User;
import com.qike.baoxiu.pojo.UserExample;
import com.qike.baoxiu.service.impl.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;

/**
 * @program: baoxiu
 * @author: Eric
 * @create: 2020-06-09 15:31
 */
   // 2.开启定时任务
public class ScheduleTask {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageService messageService;


        //3.添加定时任务
        @Scheduled(cron = "0 15 10 LW * ?")
        //或直接指定时间间隔，例如：5秒
        //@Scheduled(fixedRate=5000)
        private void configureTasks() {
            UserExample userExample = new UserExample();
            userExample.createCriteria().andRoleEqualTo(1).andStatusEqualTo(StatusEnums.NO.getEcode());
            List<User> users = userMapper.selectByExample(userExample);
            for (User user : users) {
                Message message = new Message();
                message.setAddTime(new Date());
                message.setUid(user.getId() );
                message.setTitle("月报提醒");
                message.setContent("月底了,记得上传月报哦,如已上传请忽略");
                message.setIsRead(0);
                messageMapper.insert(message);
                messageService.sendMessageOrder(user.getId());
            }


        }
}